package com.yn.module.spider.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.yn.module.spider.init.data.DataInit;
import com.yn.module.spider.pojo.DicSpider;
import com.yn.module.spider.service.*;
import com.yn.module.spider.utils.constant.Const;
import com.yn.module.spider.utils.constant.OptEnum;
import com.yn.module.spider.web.dto.SpiderMonitorDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/spider/NationMedicare/dataProcess")
@Api(tags = "爬取国家医保局相关内容")
@ApiSupport(order = 1001)
@Slf4j
public class SpiderNationController {

    @Resource
    private NatmcMaterialService natmcMaterialService;

    @ApiOperation(httpMethod = Const.POST_METHOD, value = "进度监控|01-医保耗材分类与代码]")
    @ApiOperationSupport(order = 9999)
    @PostMapping("/NatmcMaterialService")
    public Map<String, Object> dataProcess_NatmcMaterialService(@RequestBody SpiderMonitorDTO dto) {
        dto.setPages(getTotalPages(OptEnum.OpearionType_Main, OptEnum.SpiderOpertion_NationMedicare_Material));
        return natmcMaterialService.queryData4Monitor(dto);
    }

    @Resource
    private NatmcMaterialDetailService natmcMaterialDetailService;

    @ApiOperation(httpMethod = Const.POST_METHOD, value = "进度监控|01-医保耗材分类与代码--明细表]")
    @ApiOperationSupport(order = 9998)
    @PostMapping("/NatmcMaterialDetailService")
    public Map<String, Object> dataProcess_NatmcMaterialDetailService(@RequestBody SpiderMonitorDTO dto) {
        dto.setPages(getTotalPages(OptEnum.OpearionType_Detail, OptEnum.SpiderOpertion_NationMedicare_MaterialDetail));
        return natmcMaterialDetailService.queryData4Monitor(dto);
    }

    @Resource
    private NatmcDrugService natmcDrugService;

    @ApiOperation(httpMethod = Const.POST_METHOD, value = "进度监控|02-医保药品分类与代码]")
    @ApiOperationSupport(order = 9997)
    @PostMapping("/natmcDrugService")
    public Map<String, Object> dataProcess_natmcDrugService(@RequestBody SpiderMonitorDTO dto) {
        dto.setPages(getTotalPages(OptEnum.OpearionType_Main, OptEnum.SpiderOpertion_NationMedicare_Drug));
        return natmcDrugService.queryData4Monitor(dto);
    }

    @Resource
    private NatmcServService natmcServService;

    @ApiOperation(httpMethod = Const.POST_METHOD, value = "进度监控|03-全国医疗服务项目]")
    @ApiOperationSupport(order = 9996)
    @PostMapping("/natmcServService")
    public Map<String, Object> dataProcess_natmcServService(@RequestBody SpiderMonitorDTO dto) {
        dto.setPages(getTotalPages(OptEnum.OpearionType_Main, OptEnum.SpiderOpertion_NationMedicare_Serv));
        return natmcServService.queryData4Monitor(dto);
    }

    @Resource
    private NatmcMedicareMaterialService natmcMedicareMaterialService;

    @ApiOperation(httpMethod = Const.POST_METHOD, value = "进度监控|04-医保医用耗材分类与代码]")
    @ApiOperationSupport(order = 9995)
    @PostMapping("/natmcMedicareMaterialService")
    public Map<String, Object> dataProcess_natmcMedicareMaterialService(@RequestBody SpiderMonitorDTO dto) {
        dto.setPages(getTotalPages(OptEnum.OpearionType_Main, OptEnum.SpiderOpertion_NationMedicare_MedicareMaterial));
        return natmcMedicareMaterialService.queryData4Monitor(dto);
    }

    @Resource
    private NatmcMedicareMaterialDetailService natmcMedicareMaterialDetailService;

    @ApiOperation(httpMethod = Const.POST_METHOD, value = "进度监控|04-医保医用耗材分类与代码--明细表]")
    @ApiOperationSupport(order = 9994)
    @PostMapping("/natmcMedicareMaterialDetailService")
    public Map<String, Object> dataProcess_natmcMedicareMaterialDetailService(@RequestBody SpiderMonitorDTO dto) {
        dto.setPages(getTotalPages(OptEnum.OpearionType_Detail, OptEnum.SpiderOpertion_NationMedicare_MedicareMaterialDetail));
        return natmcMedicareMaterialDetailService.queryData4Monitor(dto);
    }

    /**
     * 获取当前请求的总页数
     *
     * @return
     */
    private Integer getTotalPages(OptEnum type, OptEnum svc) {
        DicSpider spider = DataInit.DIC_SPIDER.get(type.value() + Const.DEFAULT_SPLIT + svc.value());
        return spider.getPages();
    }
}
